package org.vy.dal.dao.lookup;

/**
 * @author Ravi Shankar
 *
 */

import java.util.List;

import org.vy.dal.QueryManager;

public class VyLookupProfessionDAO {

	private static final String FIND_BY_PK = "SELECT * FROM <TABLE> WHERE PROFESSION_CODE = m_ProfessionCode";
	private static final String FIND_BY_PARAM = "SELECT * FROM <TABLE> WHERE 1 = 1 ";
	private static final String UPDATE = "UPDATE <TABLE> SET PROFESSION_CODE=m_ProfessionCode,PROFESSION_NAME=m_ProfessionName WHERE PROFESSION_CODE=m_ProfessionCode";
	private static final String INSERT = "INSERT INTO <TABLE> (PROFESSION_CODE,PROFESSION_NAME) VALUES (m_ProfessionCode,m_ProfessionName)";
	private static final String DELETE = "DELETE FROM <TABLE> WHERE PROFESSION_CODE = m_ProfessionCode";

	public static VyLookupProfessionDO findByPK(String key) throws Exception {
		VyLookupProfessionDO dto = new VyLookupProfessionDO();
		dto.setProfessionCode(key);
		dto = (VyLookupProfessionDO) QueryManager.executeQuery(dto, FIND_BY_PK);
		return dto;
	}

	@SuppressWarnings("unchecked")
	public static List<VyLookupProfessionDO> findByParam(
			VyLookupProfessionDO dto) throws Exception {
		StringBuilder sb = new StringBuilder(FIND_BY_PARAM);
		if (dto.getProfessionCode() != null) {
			sb.append(" AND PROFESSION_CODE LIKE '%" + dto.getProfessionCode()
					+ "%' ");
		}
		if (dto.getProfessionName() != null) {
			sb.append(" AND PROFESSION_NAME LIKE '%" + dto.getProfessionName()
					+ "%' ");
		}

		System.out.println(sb.toString());
		return (List<VyLookupProfessionDO>) QueryManager.executeListQuery(dto,
				sb.toString());
	}

	public static VyLookupProfessionDO insert(VyLookupProfessionDO dto)
			throws Exception {

		dto = (VyLookupProfessionDO) QueryManager.executeInsert(dto, INSERT);
		return dto;
	}

	public static VyLookupProfessionDO update(VyLookupProfessionDO dto)
			throws Exception {
		int i = QueryManager.executeUpdate(dto, UPDATE);
		if (i > 0)
			return dto;
		else
			return null;
	}

	public static VyLookupProfessionDO delete(String key) throws Exception {
		VyLookupProfessionDO dto = new VyLookupProfessionDO();
		dto.setProfessionCode(key);
		boolean i = QueryManager.executeDelete(dto, DELETE);
		if (i == true)
			return dto;
		else
			return null;
	}
}
